// pages/nav/01/index.js
import { Event, Page_inset } from "../../../pageEvent.js";
import $https from "../../../utils/api.js";
let App = getApp();
let that;
App.$https = $https;
let _static = {
  /**
   * 页面的初始数据
   */

  data: {
    //  模板需要的数据
    //  detail 列表
    detail: {},
    //  top 点击切换
    topIsStatus: false,
    // top 样式
    topIsStatusIndex: 0,
    is_loding: true,
    show: false,
    left_current: 0,
    //  node节点
    nodes_el: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  init(options, page_inset, call) {
    that = this;
    page_inset.initData("detail", {});
    that.detail();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // dom 节点挂载
    // 获取dom 节点
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
      // console.log('页面销毁');
      this.setData({
        right_mao:null
      })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},
  //获取页面内容
  detail: function () {
    App.$https
      .requestApi("/open/wx_app_api/getGoodsCategory", {})
      .then(function (r) {
        if (r.code == 200) {
          that.setData({
            list: r.data,
          });
        }
      });
  },
  //分类点击
  letBtn: function (e) {
    let index = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    that.setData({
      left_current: index,
      right_mao: id,
    });
  },
  //商品列表
  getGoosList: function (e) {
    wx.navigateTo({
      url: "/pages/product/lst/index?type=2&pid=" + e.currentTarget.dataset.id,
    });
  },
  // 右边滚动
  rightEvent(e) {
    // console.log("右边滚动");
    // console.log(e.detail.scrollTop, "距离顶部的距离");
    let { scrollTop } = e.detail;
    scrollTop = scrollTop ?? 0;
    this.compute_method();

    //
    let select_Fnc = () => {
      for (let i = 0; i < this.data.total_list.length; i++) {
        let item = this.data.total_list[i];
        //  item  高度 
        if (
          scrollTop >= item.el_height.old_height &&
          scrollTop < item.el_height.new_height
        ) {
          //  改变高亮
          this.setData({
            left_current: item.index_el - 1,
          });
          //  结束循环
          break;
        }
      }
    };
    this.data.total_list?.length && select_Fnc();
  },

  // compute
  compute_method() {
    //  获取 inTit   EcList 容器高度
    this.get_queryNodes((res) => {
      // console.log(res);
      //   计算每个容器的高度
      let total_list = [];
      let index_el = 0;
      let el_obj;
      let init_height = 0;
      res.length &&
        res.reduce((pre, prc) => {
          init_height = pre;
          pre = pre + prc.height ;
          el_obj = {
            index_el: (index_el += 1),
            el_height: {
              old_height: init_height,
              new_height: pre,
            },
          };
          total_list.push(el_obj);
          return pre;
        }, 0);
      this.setData({
        nodes_el: res,
        total_list,
      });
    },".inRoll-item");
  },
  //   获取dom 节点
  get_queryNodes(call,dom_str) {
    let query = wx.createSelectorQuery();
    this.data.nodes_el?.length ||
      query
        .selectAll(dom_str)
        .boundingClientRect((rect) => {
          call(rect);
        })
        .exec();
  },
};
Page(Object.assign(_static, Event, {}));
